package oa.hleast.xswl.service.oms.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.mapper.oms.OmsOrderCommentMapper;
import oa.hleast.xswl.pojo.domain.SysUser;
import oa.hleast.xswl.pojo.domain.oms.OmsOrderComment;
import oa.hleast.xswl.service.ISysUserService;
import oa.hleast.xswl.service.oms.OmsOrderCommentService;
import oa.hleast.xswl.service.store.StoreInfoService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author: 302
 * @date: 2021-10-26 17:41:28
 * @description: 订单评论表(OmsOrderComment)接口实现
 */
@Slf4j
@AllArgsConstructor
@Transactional
@Service("omsOrderCommentService")
public class OmsOrderCommentServiceImpl extends ServiceImpl<OmsOrderCommentMapper, OmsOrderComment> implements OmsOrderCommentService {

    private final ISysUserService sysUserService;
    private final StoreInfoService storeInfoService;

    @Override
    public IPage<OmsOrderComment> findComment(Integer page, Integer limit, Integer reviewStatus) {


        IPage<OmsOrderComment> commentPage = findComment(page, limit, null, reviewStatus, null);
        log.info("commentPage = {}", commentPage);

        return commentPage;
    }

    /**
     * 审核通过or审核驳回
     */
    @Override
    public Boolean reviewComment(Long id, Integer reviewStatus, String remark) {

        boolean update = this.update(new LambdaUpdateWrapper<OmsOrderComment>()
                .set(OmsOrderComment::getReviewStatus, reviewStatus)
                .set(OmsOrderComment::getReviewRemark, remark)
                .eq(OmsOrderComment::getId, id)
        );

        // TODO 保存审核记录

        return update;
    }

    @Override
    public IPage<OmsOrderComment> findCommentReviewForStore(Integer page, Integer limit, String spuNo, Integer reviewStatus) {
        SysUser sysUser = sysUserService.getById(RequestUtils.getUserId());
        if (ObjectUtils.isEmpty(sysUser) || ObjectUtils.isEmpty(sysUser.getStoreId())) {
            throw new BizException("账号或店铺异常");
        }

        IPage<OmsOrderComment> comment = findComment(page, limit, spuNo, reviewStatus, sysUser.getStoreId());

        log.info("comment = {}", comment);
        return comment;
    }

    @Override
    public boolean saveListComment(List<OmsOrderComment> orderCommentFormList) {
        boolean flag = true;
        for (OmsOrderComment omsOrderComment : orderCommentFormList) {
            flag = flag && this.save(omsOrderComment);
        }
        return flag;
    }

    private IPage<OmsOrderComment> findComment(Integer page, Integer limit, String spuNo, Integer reviewStatus, Long storeId) {

        LambdaQueryWrapper<OmsOrderComment> queryWrapper = new LambdaQueryWrapper<OmsOrderComment>()
                .eq(ObjectUtils.isNotEmpty(reviewStatus), OmsOrderComment::getReviewStatus, reviewStatus)
                .eq(ObjectUtils.isNotEmpty(storeId), OmsOrderComment::getStoreId, storeId)
                .orderByDesc(OmsOrderComment::getGmtCreate)
                .orderByDesc(OmsOrderComment::getGmtModified);

        if (ObjectUtils.isNotEmpty(spuNo)) {
            // TODO 获取商品souId

        }

        return this.page(new Page(page, limit), queryWrapper);

    }
}
